package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserAddressMapper;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月08日 16:05
 * @ClassName:UserServiceImpl
 * @Description: TODO
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Override
    public UserInfo login(String username, String password) {
        //校验参数
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new RuntimeException("用户名或密码不能为空!");
        }
        //登录校验
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getLoginName, username));
        if(userInfo==null||userInfo.getId()==null){
            throw new RuntimeException("用户名或密码错误!");
        }
        //判断密码
        String passwd = userInfo.getPasswd();
        //将明文进行加密
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!md5Password.equals(passwd)){
             throw new RuntimeException("用户名或密码错误");
        }
        return userInfo;
    }

    /**
     * 根据用户id查询用户的收货地址
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> getUserAddressList(Long userId) {
        return userAddressMapper
                .selectList(new LambdaQueryWrapper<UserAddress>()
                        .eq(UserAddress::getUserId, userId));
    }

    public static void main(String[] args) {
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        System.out.println(s);
    }
}
